package view;

import java.util.ArrayList;

import mdes.slick.sui.Button;
import mdes.slick.sui.Container;
import mdes.slick.sui.Display;
import mdes.slick.sui.TextField;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;
import model.Wrapper;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import controller.BestlistManager;


/*
 * 
 *  When mister dies, it will launch this state. this state will normally display an image with "game over", you three buttons : "Try again", "load score", "exit".
 * 
 */
public class MisterJumpGameover extends BasicGameState implements ActionListener  {
	
	private int stateID = -1;
	
	private Image gameOverImage = null;
	private Display display = null;
	
	private Button[] buttons = null;	
    private int buttonYLocations[] = {260,260,260};
    private int buttonXLocations[] = {30,120,190};
    
    private TextField[] textfields = null;
    private int textfieldYLocations[] = {220,220};
    private int textfieldXLocations[] = {30,170};
    
    private boolean TryAgainBtnPressed = false;
	private boolean UploadHighscoreBtnPressed = false;
    private boolean BackBtnPressed = false;
	    
	String fontPath = "/data/VTKSAnimal2.ttf";
	UnicodeFont font;
	
	public MisterJumpGameover(int stateID) {
		// TODO Auto-generated constructor stub
		this.stateID = stateID;
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		if (TryAgainBtnPressed)
		{
			Wrapper.reset();
			game.enterState(MisterJumpMain.GAMEPLAYSTATE);
			TryAgainBtnPressed = false;
			
		}
		if (BackBtnPressed) 
		{
			Wrapper.reset();
			game.enterState(MisterJumpMain.MAINMENUSTATE, new FadeOutTransition(), new FadeInTransition());
			BackBtnPressed = false;
		}
		if (UploadHighscoreBtnPressed)
		{
			int errorcode = new BestlistManager(textfields[0].getText(), textfields[1].getText()).postHighscore(Wrapper.getInstance().getPoints());
			UploadHighscoreBtnPressed = false;
		}
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return stateID;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame game)
			throws SlickException {
		font = new UnicodeFont(fontPath, 20, false, false);
		font.addAsciiGlyphs();  
		font.addGlyphs(400, 600);
		font.getEffects().add(new ColorEffect(java.awt.Color.BLACK));
		font.loadGlyphs();  
		
		gameOverImage = new Image("data/GameOver.png");
	    display = new Display(gc);
	    
	    buttons = new Button[3];
	    ArrayList buttonNames = new ArrayList(); buttonNames.add(0, "Try Again"); buttonNames.add(1,"Back"); buttonNames.add(2,"Hochladen!");
	    
        textfields = new TextField[2];
        ArrayList textfieldNames = new ArrayList(); textfieldNames.add(0, "Benutzername"); textfieldNames.add(1, "passwort        ");
	    
	    Container container = new Container(); container.setSize(320, 512); container.setLocation(0, 0); container.setOpaque(false); 
	        
	    for (int i = 0; i < buttons.length; i++) {
	    	buttons[i] = new Button(buttonNames.get(i).toString());
			buttons[i].setLocation(buttonXLocations[i],buttonYLocations[i]);
			buttons[i].addActionListener(this);
			buttons[i].setActionCommand(buttonNames.get(i).toString());
			buttons[i].pack();
			container.add(buttons[i]);
		}
	    
        for (int i = 0; i < textfields.length; i++) {
			textfields[i] = new TextField(textfieldNames.get(i).toString());
			textfields[i].setLocation(textfieldXLocations[i], textfieldYLocations[i]); 
			textfields[i].addActionListener(this);
			textfields[i].setActionCommand(textfieldNames.get(i).toString());
			container.add(textfields[i]);
		}
        
	    //make textfield for password masked
	    textfields[1].setMaskCharacter('*');
	    textfields[1].setMaskEnabled(true);
	        
	        
	    display.add(container);
	   }
		// TODO Auto-generated method stub
		
	

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		gameOverImage.draw(0, 0);
	    display.render(container, g);
	    font.drawString(30, 180, "Your Score: " + (int)(Wrapper.getInstance().getPoints()*100));
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if ("Try Again".equals(arg0.getActionCommand())) {
			TryAgainBtnPressed = true;
		}else if ("Back".equals(arg0.getActionCommand())) {
			BackBtnPressed = true;
		}else if ("Hochladen!".equals(arg0.getActionCommand())){
			UploadHighscoreBtnPressed = true;
		}
	}
}
	

